<template>
	<view class="homeLayout bg">
		<nav-bar title="推荐"></nav-bar>
		<uv-skeletons :loading="bannersLoading" :skeleton="bannerSkeleton">
			<view class="banner">
				<swiper indicator-dots indicator-color="rgba(255,255,255,0.5)" 
				indicator-active-color="#fff" autoplay :interval="3000" :duration="1000" circular>
					<swiper-item v-for="(item, index) in banners" :key="item._id">
						<image :src="cloudIdToUrl(item.picurl,{w:'700rpx',h:'345rpx'})" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>
		</uv-skeletons>
		
		<uv-skeletons :loading="noticesLoading" :skeleton="noticesSkeleton">
			<view class="notice">
				<view class="left">
					<uni-icons type="sound-filled" size="22"></uni-icons>
					<text class="title">公告</text>
				</view>
				<view class="center">
					<swiper autoplay :interval="3000" :duration="1000" vertical circular>
						<swiper-item v-for="(item, index) in notices" :key="item._id" @click="toNotice(item._id)">
							<view class="content-wrapper">
								<text class="content">{{item.title}}</text>
							</view>
						</swiper-item>
					</swiper>
				</view>
				<view class="right">
					<uni-icons type="right" size="20" color="#1ab482"></uni-icons>
				</view>
			</view>
		</uv-skeletons>
		
		<view class="select">
			<common-title>
				<template #title>每日推荐</template>
				<template #right>
					<view class="date">
						<uni-icons type="calendar" size="20"></uni-icons>
						<uni-dateformat :date="Date.now()" format="dd号" class="text"></uni-dateformat>
					</view>
				</template>
			</common-title>
			<uv-skeletons :loading="selectLoading" :skeleton="selectSkeleton">
				<view class="content">
					<scroll-view scroll-x>
						<view class="box" v-for="(item, index) in randomWalls" :key="item._id">
							<image :src="cloudIdToUrl(item.picurl,{w:'200rpx',h:'450rpx'})" mode="aspectFill" @click="navPre(item._id)"></image>
						</view>
					</scroll-view>
				</view>
			</uv-skeletons>
		</view>
		
		<view class="theme">
			<common-title>
				<template #title>专题精选</template>
				<template #right>
					<navigator url="/pages/classify/classify" open-type="switchTab" class="more">
						More+
					</navigator>
				</template>
			</common-title>
			<uv-skeletons :loading="classesLoading" :skeleton="classesSkeleton">
				<view class="content">
					<theme-item v-for="(item, index) in classes" :key="index" :value="item"></theme-item>
					<theme-item isMore></theme-item>
				</view>
			</uv-skeletons>
		</view>
		<!-- #ifdef H5 -->
		<nav-fill></nav-fill>
		<!-- #endif -->
		
	</view>
</template>

<script setup>
import {ref} from 'vue'
import { apiGetClassifyList, apiGetSwiperList, apiGetNoticeList, apiGetImageList } from '@/api/index.js'
import { modifyFilenameAndExtension } from '@/utils/common.js'
import { onShareAppMessage, onShareTimeline, onPullDownRefresh } from '@dcloudio/uni-app'
import {cloudIdToUrl} from '@/utils/common.js'


const toNotice = (id) => {
	uni.navigateTo({
		url:'/pages/notice/detail?id='+id
	})
}

const bannersLoading = ref(true)
const bannerSkeleton = ref([{
	type: 'line',
	num: 1,
	gap: '20rpx',
	style: ['width: 690rpx; margin-top: 30rpx; margin-left: 30rpx; height: 340rpx']
}])
				
const banners = ref([])
const getBanners = async () => {
	let data = await apiGetSwiperList()
	banners.value = data
	bannersLoading.value = false
}

const noticesLoading = ref(true)
const noticesSkeleton = ref([{
	type: 'line',
	num: 1,
	gap: '20rpx',
	style: ['width: 690rpx; margin: 30rpx; height:80rpx']
}])

const notices = ref([])
const getNotices = async () => {
	let data = await apiGetNoticeList()
	notices.value = data.list
	noticesLoading.value = false
}

const selectLoading = ref(true)
const selectSkeleton = ref([{
	type: 'flex',
	children: [{
			type: 'custom',
			style: 'width:200rpx;height:430rpx;margin:30rpx 0 0 30rpx;'
		}, {
			type: 'custom',
			style: 'width:200rpx;height:430rpx;margin:30rpx 0 0 30rpx;'
		}, {
			type: 'custom',
			style: 'width:200rpx;height:430rpx;margin:30rpx 0 0 30rpx;marginRight:30rpx'
		}]
	}])

const randomWalls = ref([])
const getRandomWall = async () => {
	let data = await apiGetImageList({random:true, pageSize:12})
	randomWalls.value = data.list
	selectLoading.value = false
	
	// 存储images数组
	let arrs = randomWalls.value.map(item=>{
		return item
	})
	
	uni.setStorageSync('priviews',arrs)
}

// 跳转到预览页面
const navPre = (id)=>{
	const value = uni.getStorageSync('priviews')
	if(!value) {
		uni.setStorageSync('priviews',randomWalls.value)
	}
	uni.navigateTo({
		url:"/pages/preview/preview?id="+id
	})
}

const classesLoading = ref(true)
const classesSkeleton = ref([{
	type: 'flex',
	children: [{
			type: 'custom',
			style: 'width:200rpx;height:340rpx;margin:30rpx 0 0 30rpx;'
		}, {
			type: 'custom',
			style: 'width:200rpx;height:340rpx;margin:30rpx 0 0 30rpx;'
		}, {
			type: 'custom',
			style: 'width:200rpx;height:340rpx;margin:30rpx 0 0 30rpx;marginRight:30rpx'
		}]
	}])

const classes = ref([])
const getClasses = async () => {
	let data = await apiGetClassifyList({select : true})
	classes.value = data.list.slice(0, 8)
	classesLoading.value = false
}

// 分享给好友
onShareAppMessage(e=>{
	return {
		title: "咸虾米壁纸",
		path:'/pages/index/index'
	}
})

// 分享朋友圈
onShareTimeline(e=>{
	return {
		title: '咸虾米壁纸'
	}
})



getBanners()
getClasses()
getNotices()
getRandomWall()

onPullDownRefresh(async () => {
	getBanners()
	getClasses()
	getNotices()
	getRandomWall()
	uni.stopPullDownRefresh()
})


</script>


<style lang="scss" scoped>
.bg{
	background: 
	linear-gradient(to bottom, rgba(0,0,0,0,), #fff 500rpx),
	linear-gradient(to right, #beecd8 20%, #F4E2D8);
	height: 80vh;
}
.homeLayout {
	.banner {
		width: 100%;
		padding: 10rpx 0;
		swiper{
			width: 100%;
			height: 340rpx;
			&-item {
				padding: 0 30rpx;
				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}
	.notice{
		width: calc(100% - 60rpx);
		height: 80rpx;
		line-height: 80rpx;
		margin: 20rpx auto;
		display: flex;
		background: #f9f9f9;
		border-radius: 20rpx;
		.left{
			width: 140rpx;
			display: flex;
			justify-content:center;
			align-items: center;
			:deep(){
				.uni-icons{
					color: $brand-theme-color !important;
				}
			}
			.title{
				color: $brand-theme-color;
				font-weight: 600;
				font-size: 28rpx;
			}
		}
		.center{
			flex: 1;
			swiper{
				width: 450rpx;
				height: 100%;
				&-item {
					height: 100%;
					.content{
						font-size: 28rpx;
						color: $text-color-sub;
						display: block;  /* 将行内元素转为块级元素以应用宽度限制 */
						white-space: nowrap;  /* 禁止换行 */
						overflow: hidden;  /* 隐藏溢出内容 */
						text-overflow: ellipsis;  /* 显示省略号 */
					}
				}
			}
		}
		.right{
			width: 70rpx;
			display: flex;
			justify-content:center;
			align-items: center;
		}
	}
	.select{
		padding-top: 30rpx;
		.content{
			width: calc(100% - 30rpx);
			margin-left: 30rpx;
			margin-top: 30rpx;
			scroll-view{
				white-space: nowrap;
				.box{
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
				.box:last-child{
					margin-right: 30rpx;
				}
			}
		}
		.date{
			color: $brand-theme-color;
			font-size: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			:deep(){
				.uni-icons{
					color: $brand-theme-color !important;
				}
			}
			.text{
				margin-left: 5rpx;
			}
		}
	}
	.theme{
		padding: 50rpx 0 20rpx 0;
		.more{
			color: $brand-theme-color;
			font-size: 32rpx;
		}
		.content{
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid; /* Gid布局 */
			gap: 15rpx; /* Gid布局-设置gap间距 */
			grid-template-columns: repeat(3,1fr); /* Gid布局-设置按照每行3列，每列自动计算 */
		}
	}
}
</style>
